/*
 * Xiazaiwancheng.java
 *
 * Created on __DATE__, __TIME__
 */

package Windows.DownLoads;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import javax.swing.border.LineBorder;

import GroupWare.DownloadTask;
import Tools.IMG;
import Tools.JLabelStyle;
import Tools.Units;
import Windows.PicPanel;
import XML.XMLNode;

/**
 * 
 * @author __USER__
 */
public class ShowDownWindow extends javax.swing.JFrame implements MouseListener {
	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				XMLNode xf = new XMLNode(null, "task");
				xf.put("State", "ok");
				xf.put("name", "QQ2009.EXE");
				xf.put("size", "52665959");
				xf.put("MD5", "DTGY789IO03FTGBNHJ");
//				ShowDownWindow xzw = new ShowDownWindow(xf);
//				xzw.start();
			}
		});
	}
	private boolean doing;
	private int exists;
	public final PicPanel mp;
	private JLabelStyle jlsAction = (JLabelStyle) JLabelStyle.getStyleListener();

	private DownloadTask df;

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JLabel jLabel2;

	private javax.swing.JLabel jLabel4;

	private javax.swing.JLabel jLabel6;

	public javax.swing.JLabel jlClose;

	// GEN-END:initComponents

	public javax.swing.JLabel jlMD5;

	public javax.swing.JLabel jlOpenDir;

	// private void jLabel10MouseEntered(java.awt.event.MouseEvent evt) {
	// // TODO add your handling code here:
	// jlOpenDir.setForeground(Color.blue);
	// jlOpenDir.setCursor(new Cursor(HAND_CURSOR));
	// }
	//
	// private void jLabel8MouseExited(java.awt.event.MouseEvent evt) {
	// // TODO add your handling code here:
	// jlOpenFile.setForeground(Color.black);
	// }
	//
	// private void jLabel8MouseEntered(java.awt.event.MouseEvent evt) {
	// // TODO add your handling code here:
	// jlOpenFile.setForeground(Color.blue);
	// jlOpenFile.setCursor(new Cursor(HAND_CURSOR));
	// }
	//
	// private void jLabel8MouseClicked(java.awt.event.MouseEvent evt) {
	// // TODO add your handling code here:
	//
	// }

	public javax.swing.JLabel jlOpenFile;

	public javax.swing.JLabel jlSize;
	public javax.swing.JLabel jlTitle;
	public javax.swing.JLabel jlname;
	/** Creates new form Xiazaiwancheng */
	public ShowDownWindow(DownloadTask df) {
		this.df = df;
		mp = new PicPanel(IMG.Win);
		mp.setBorder(new LineBorder(Color.BLACK));
		this.setContentPane(mp);
		initComponents();
////		setSize(300,200);
//		pack();
		setAlwaysOnTop(true);
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		mp.addMouseListener(this);

		double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		int x = (int) ((width - ShowDownWindow.this.getSize().getWidth()) - 5);
		int y = (int) ((height) - 30);
		ShowDownWindow.this.setLocation(x, y);

		showTitle();
		jlname.setText(df.getXML().get("name"));
		jlSize.setText(Units.byteCondense(Long.parseLong(df.getXML().get("size"))));
		jlMD5.setText(df.getXML().FLAG.toUpperCase());
	}
	public void Close() {

		new Thread(new Runnable() {
			@Override
			public void run() {
				doing = true;
				int Y = (int) (getLocation().y + getSize().getHeight());// 终点顶距

				while (getLocation().y < Y) {
					setLocation(new Point(getLocation().x, getLocation().y + 10));
					float f = (Y - getLocation().y) / (float) (getSize().height);
					f = f > 1 ? 1 : f < 0 ? 0 : f;
					// System.out.println((Y-getLocation().y)+"/"+getSize().height
					// );
					if (ShowDownWindow.this.isUndecorated()) {
						com.sun.awt.AWTUtilities.setWindowOpacity(ShowDownWindow.this, f);// --窗口透明度设置
					}
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
					}
				}
				ShowDownWindow.this.setVisible(false);
				ShowDownWindow.this.dispose();
				doing = false;
			}
		}).start();
	}
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jlTitle = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jlname = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jlSize = new javax.swing.JLabel();
		jLabel6 = new javax.swing.JLabel();
		jlMD5 = new javax.swing.JLabel();
		jlOpenFile = new javax.swing.JLabel();
		jlClose = new javax.swing.JLabel();
		jlOpenDir = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("任务状态");
		setUndecorated(true);

		jlTitle.setFont(new Font("宋体", Font.BOLD, 14));// 标题大小

		jlTitle.setText("\u5df2\u4e0b\u8f7d\u5b8c\u6210");
		// jlTitle.setFont(new Font());
		jLabel2.setText("\u6587\u4ef6\u540d\uff1a");

		jlname.setText("ssssssssssssssssssssss");

		jLabel4.setText("\u6587\u4ef6\u5927\u5c0f\uff1a");

		jlSize.setText("yyyyyyyyyyyyyyyyyyyyyy");

		jLabel6.setText("MD5\uff1a");

		jlMD5.setText("zzzzzzzzzzzzzzzzzzzzzzz");

		jlOpenFile.setText("\u6253\u5f00\u6587\u4ef6");

		jlOpenFile.addMouseListener(jlsAction);
		jlOpenDir.addMouseListener(jlsAction);
		jlClose.addMouseListener(jlsAction);

		jlClose.setText("\u5173\u95ed");
		jlClose.addMouseListener(this);
		jlOpenFile.addMouseListener(this);
		jlOpenDir.addMouseListener(this);
		jlOpenDir.setText("\u6253\u5f00\u6587\u4ef6\u5939");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addGap(32, 32, 32).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jLabel6).addComponent(jLabel2).addComponent(jLabel4).addComponent(jlOpenFile, javax.swing.GroupLayout.Alignment.LEADING)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false).addComponent(jlMD5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jlSize, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jlname, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jlTitle)).addGroup(layout.createSequentialGroup().addComponent(jlOpenDir).addGap(18, 18, 18).addComponent(jlClose))).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addGap(20, 20, 20).addComponent(jlTitle).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2).addComponent(jlname)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel4).addComponent(jlSize)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel6).addComponent(jlMD5)).addGap(36, 36, 36)
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jlOpenDir).addComponent(jlClose).addComponent(jlOpenFile, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	private void jLabel9MouseEntered(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		jlClose.setForeground(Color.blue);
		jlClose.setCursor(new Cursor(HAND_CURSOR));
	}
	private void jLabel9MouseExited(java.awt.event.MouseEvent evt) {
		// TODO add your handling code here:
		jlClose.setForeground(Color.black);
	}
	@Override
	public void mouseClicked(MouseEvent me) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		this.mp.actionEvent(true);

		exists = -1;// 鼠标进入线程暂停

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		this.mp.actionEvent(false);

		exists = 0;// 鼠标离开开始计时
	}

	@Override
	public void mousePressed(MouseEvent me) {
		// TODO Auto-generated method stub
		// this.setVisible(false);
		Object obj = me.getSource();
		if (obj.getClass() == JLabel.class) {
			JLabel jl = (JLabel) obj;
			System.out.println(jl);
			if (jl == jlOpenFile) {
				df.open();
				System.out.println("OPen:" + df);
			} else if (jl == jlOpenDir) {
				df.openDir();
				System.out.println("OPenDir:" + df);
			}
		}

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		// 关闭效果下滑，不直接消失。
		if (!doing)
			Close();

	}

	public void showTitle() {
		String str = df.getXML().get("State").toLowerCase();
		if (str.equals("new")) {
			str = "创建任务";
			jlOpenFile.setEnabled(false);
		} else if (str.equals("ok")) {
			str = "完成任务";
			jlOpenFile.setEnabled(true);
		}
		jlTitle.setText(str);
	}

	public void start() {

		this.setVisible(true);
		if (this.isUndecorated()) {
			com.sun.awt.AWTUtilities.setWindowOpacity(ShowDownWindow.this, 0.1f);// 窗口透明度设置
		}
		new Thread(new Runnable() {
			@Override
			public void run() {
				doing = true;
				int Y = (int) (getLocation().y - getSize().getHeight());
				try {
					Thread.sleep(500);// 浮出前停顿
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
				while (getLocation().y > Y) {
					setLocation(new Point(getLocation().x, getLocation().y - 10));
					float f = (getSize().height - (getLocation().y - Y)) / (float) (getSize().height);
					f = f > 1 ? 1 : f < 0 ? 0 : f;
					// System.out.println((getLocation().y-Y)+"/"+getSize().height
					// );
					if (ShowDownWindow.this.isUndecorated()) {
						com.sun.awt.AWTUtilities.setWindowOpacity(ShowDownWindow.this, f);// --窗口透明度设置
					}
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
					}
				}

				// ===========3秒后自动关闭============

				exists = 0;
				new Thread(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						while (exists == -1 || (exists++) < 3)
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
							}
						if (ShowDownWindow.this.isVisible())
							Close();
					}
				}).start();// 启动时钟关闭窗口线程
				doing = false;
			}
		}).start();
	}

	// End of variables declaration//GEN-END:variables

}